package main

import (
	"log"
	"net/http"

	"order-validation-api/handlers"
	"order-validation-api/middleware"
	"order-validation-api/services"
)

func main() {
	// 创建验证服务
	validationService := services.NewOrderValidationService()
	
	// 创建处理器
	orderHandler := handlers.NewOrderHandler(validationService)

	// 创建路由
	mux := http.NewServeMux()
	
	// 注册路由
	mux.HandleFunc("/api/v1/orders/validate", orderHandler.ValidateOrder)
	mux.HandleFunc("/api/v1/orders/validate-batch", orderHandler.ValidateOrderBatch)
	mux.HandleFunc("/api/v1/clients", orderHandler.GetSupportedClients)
	mux.HandleFunc("/health", orderHandler.Health)

	// 应用中间件
	handler := middleware.LoggingMiddleware(
		middleware.CORSMiddleware(
			middleware.RateLimitMiddleware(mux),
		),
	)

	// 启动服务器
	port := ":8080"
	log.Printf("Server starting on port %s", port)
	log.Printf("API endpoints:")
	log.Printf("  POST /api/v1/orders/validate - Validate single order")
	log.Printf("  POST /api/v1/orders/validate-batch - Validate multiple orders")
	log.Printf("  GET  /api/v1/clients - Get supported clients")
	log.Printf("  GET  /health - Health check")

	if err := http.ListenAndServe(port, handler); err != nil {
		log.Fatal("Server failed to start:", err)
	}
}